package com.itheima.web.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.domain.Category;

import com.itheima.factory.BeanFactory;
import com.itheima.service.CategoryService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/categoryServlet")
public class CategoryServlet extends BaseServlet {
    CategoryService categoryService = (CategoryService) BeanFactory.getBean("categoryService");

    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
        List<Category> categoryList = categoryService.findAll();

        String value = new ObjectMapper().writeValueAsString(categoryList);

        resp.getWriter().write(value);
    }

    /*@Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        if (StringUtils.equals(action, "findAll")) {
            findAll(req, resp);
        }
    }
    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Category> categoryList = categoryService.findAll();

        String value = new ObjectMapper().writeValueAsString(categoryList);

        resp.getWriter().write(value);

    }*/

}
